package jp.sourceforge.akjrcp.flvtools.explorer;

import java.io.File;

import jp.sourceforge.akjrcp.akj_betas.Messages;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchSite;

public class RenameDialog extends Dialog{

	private IWorkbenchSite site;
	
	private File files[]=new File[0];
	private Text texts[];
	protected RenameDialog(IWorkbenchSite site) {
		super(site.getShell());
		this.site=site;
	}

	
	public boolean isOpened(){
		return this.getShell()!=null && !this.getShell().isDisposed();
	}
protected Point getInitialSize(){
	return new Point(320,240);
	}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(Messages.getString("explorer.convertdialog"));
}
public void closeWindow(){
	
	this.close();
}

private String toLabelName(File file){
	String name=file.getName();
	if(name.toLowerCase().endsWith(".flv")){
		name=name.substring(0,name.length()-4);
	}
	return name;
}

private String toFileName(String label,File file){
	String name=file.getName();
	String newName=label;
	if(name.toLowerCase().endsWith(".flv")){
		newName+=name.substring(name.length()-4);
	}
	return newName;
}
protected Control createDialogArea(Composite parent){
	Composite root = (Composite)super.createDialogArea(parent);
	root.setLayout(new FillLayout());
	ScrolledComposite sc=new ScrolledComposite(root,SWT.V_SCROLL|SWT.H_SCROLL);
	Composite composite=new Composite(sc,SWT.NONE);
	Layout layout=new GridLayout(2,false);
	composite.setLayout(layout);
	texts=new Text[files.length];
	for (int i = 0; i < files.length; i++) {
		Label label=new Label(composite,SWT.NULL);
		String name=toLabelName(files[i]);
		label.setText(name);
		texts[i]=new Text(composite,SWT.NULL);
		texts[i].setText(name);
		texts[i].setLayoutData(new GridData(GridData.FILL_HORIZONTAL));//
	}
	texts[0].selectAll();
	composite.pack();
	//log.info(composite.getSize());
	sc.setContent(composite);
    sc.setMinSize(composite.getSize().x,composite.getSize().y);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
	
	sc.layout();
	return root;
}

protected void createButtonsForButtonBar(Composite parent) {
   
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
            false);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL,
            true);
}


public File[] getFiles() {
	return files;
}


public void setFiles(File[] files) {
	this.files = files;
}

protected void okPressed() {
	
    setReturnCode(OK);

	for (int i = 0; i < files.length; i++) {
		String newName=toFileName(texts[i].getText(),files[i]);
		if(!newName.equals(files[i].getName())){
		File newFile=new File(files[i].getParent(),newName);
		if(!newFile.exists()){
		boolean result=files[i].renameTo(newFile);
		ExplorerView.log.info(result+":rename from "+files[i]+" to "+newFile);
		}
		}
	}
    close();
}



}
